package com.qiyadeng.wechat.roo.model;
import java.util.ArrayList;
import java.util.List;

import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", table = "STORE_AREA")
@RooDbManaged(automaticallyDelete = true)
public class StoreArea {

	//得到所有的城市
	public static List<String> getAllCity(){
		List<String> list = 	entityManager().createQuery("select distinct(o.cityName) from StoreArea o",String.class).getResultList();
		return list;
	}
	
	//得到一个城市所有的店
	public static List<Store> getStoresOneCity(String cityName){
		List<StoreArea> cityAreas = entityManager().createQuery("select o from StoreArea o where o.cityName=?1",StoreArea.class)
		.setParameter(1, cityName)
		.getResultList();
		List<String> codeList = new ArrayList<String>();
		for (StoreArea storeArea : cityAreas) {
			codeList.add(storeArea.getStoreCode());
		}
		List<Store> storeList = entityManager().createQuery("select o from Store o where o.storeCode in (?1)",Store.class)
		.setParameter(1, codeList)
		.getResultList();
		
		return storeList;
	}
}

